package com.gm.wholesale.module.report.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.gm.commonlib.base.baseAdapter.CommonAdapter;
import com.gm.commonlib.base.baseAdapter.ViewHolder;
import com.gm.commonlib.base.baseBean.BasePageData;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.commonlib.views.popupwindow.Popupwindow;
import com.gm.wholesale.R;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.good.bean.GoodBean;
import com.gm.wholesale.module.report.bean.ReportCustomBean;
import com.gm.wholesale.module.report.bean.ReportGoodBean;
import com.gm.wholesale.module.report.model.ReportModel;

public class ShowCustomReportView extends LinearLayout {
    private Context mContext;
    private ImageView closeImage;
    private ListView listView;

    private TextView lable1Tv;

    private TextView mTitleTv;

    public ShowCustomReportView(Context context) {
        super(context);
        initView(context);
    }

    public ShowCustomReportView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.view_showcustom_report, this);
        lable1Tv = findViewById(R.id.lable1Tv);
        mTitleTv=findViewById(R.id.titleTv);
        closeImage = findViewById(R.id.closeImage);
        closeImage.setOnClickListener(view -> Popupwindow.hiddenPop());
        listView = findViewById(R.id.listView);
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Popupwindow.hiddenPop();
            }
        });
    }

    public void showGoodReport(GoodBean goodBean, IBaseView view) {
        lable1Tv.setText("客户");
        mTitleTv.setText(goodBean.getName());
        ReportModel.getReportSKU(view, goodBean.getId(), new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(BasePageData<ReportCustomBean> resp) {
                if (resp != null && resp.getData() != null
                        && resp.getData().getDatas() != null) {
                    listView.setAdapter(new CommonAdapter<>(mContext, resp.getData().getDatas(), R.layout.list_view_custom_report_item) {
                        @Override
                        public void convert(ViewHolder viewHolder, ReportCustomBean bean) {
                            viewHolder.setText(R.id.goodNameTv, bean.getCustomerName());
                            viewHolder.setText(R.id.goodNumTv, bean.getQuantity());
                            viewHolder.setText(R.id.goodPriceTv, bean.getSalesAmount());
                        }
                    });
                }

            }
        });
    }


    public void showCustom(CustomBean customBean, IBaseView view) {
        mTitleTv.setText(customBean.getName());
        ReportModel.getReportCustomerSKU(view, customBean.getId(), new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(BasePageData<ReportGoodBean> resp) {
                if (resp != null && resp.getData() != null
                        && resp.getData().getDatas() != null) {
                    listView.setAdapter(new CommonAdapter<>(mContext, resp.getData().getDatas(), R.layout.list_view_custom_report_item) {
                        @Override
                        public void convert(ViewHolder viewHolder, ReportGoodBean bean) {
                            viewHolder.setText(R.id.goodNameTv, bean.getSkuName());
                            viewHolder.setText(R.id.goodNumTv, bean.getQuantity() + bean.getUnit());
                            viewHolder.setText(R.id.goodPriceTv, bean.getSalesAmount());
                        }
                    });
                }
            }

        });
    }


}
